package com.caregiver.watch.business.user.service.impl;

import com.caregiver.watch.business.user.dao.UserAlarmClockConfigDao;
import com.caregiver.watch.business.user.model.dto.AddUserAlarmClockConfigParam;
import com.caregiver.watch.business.user.model.dto.UpdateUserAlarmClockConfigParam;
import com.caregiver.watch.business.user.model.po.UserAlarmClockConfig;
import com.caregiver.watch.business.user.model.vo.UserAlarmClockConfigVo;
import com.caregiver.watch.business.user.service.UserAlarmClockService;
import com.caregiver.watch.business.user.utils.UserInfoUtils;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.utils.collection.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ONE;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 用户闹钟服务
 * @Author 疆戟
 * @Date 2025/8/24 16:09
 * @Version 1.0
 */
@Service
@Slf4j
public class UserAlarmClockServiceImpl implements UserAlarmClockService {

    @Autowired
    private UserAlarmClockConfigDao userAlarmClockConfigDao;

    @Override
    public List<UserAlarmClockConfigVo> queryUserAlarmClockConfigs() {
        Long userId = UserInfoUtils.getUserId();
        List<UserAlarmClockConfig> userAlarmClockConfigs = userAlarmClockConfigDao.selectByUserId(userId);
        return ListUtils.map(userAlarmClockConfigs, UserAlarmClockConfigVo::build);
    }

    @Override
    public Boolean addUserAlarmClockConfig(AddUserAlarmClockConfigParam param) {
        if (Objects.isNull(param.getAlarmTime())) {
            throw new BusinessException("请选择闹钟时间");
        }
        UserAlarmClockConfig userAlarmClockConfig = new UserAlarmClockConfig();
        userAlarmClockConfig.setUserId(UserInfoUtils.getUserId());
        userAlarmClockConfig.setAlarmTime(param.getAlarmTime());
        userAlarmClockConfig.setRepeatDaysSet(param.getRepeatDays());
        userAlarmClockConfig.setIsEnabled(BooleanUtils.isTrue(param.getIsEnabled()) ? ONE : ZERO);
        return userAlarmClockConfigDao.insert(userAlarmClockConfig) > ZERO;
    }

    @Override
    public Boolean deleteUserAlarmClockConfig(Long configId) {
        if (Objects.isNull(configId)) {
            throw new BusinessException("请选择要删除的闹钟");
        }
        Long userId = UserInfoUtils.getUserId();
        return userAlarmClockConfigDao.delete(configId, userId) > ZERO;
    }

    @Override
    public Boolean updateUserAlarmClockConfig(UpdateUserAlarmClockConfigParam param) {
        if (Objects.isNull(param.getId())) {
            throw new BusinessException("请选择要修改的闹钟");
        }
        if (Objects.isNull(param.getAlarmTime())) {
            throw new BusinessException("请选择闹钟时间");
        }
        UserAlarmClockConfig userAlarmClockConfig = new UserAlarmClockConfig();
        userAlarmClockConfig.setId(param.getId());
        userAlarmClockConfig.setAlarmTime(param.getAlarmTime());
        userAlarmClockConfig.setRepeatDaysSet(param.getRepeatDays());
        return userAlarmClockConfigDao.update(userAlarmClockConfig) > ZERO;
    }

    @Override
    public Boolean updateUserAlarmClockConfigEnabledStatus(Long configId, Boolean isEnabled) {
        if (Objects.isNull(configId)) {
            throw new BusinessException("请选择要修改的闹钟");
        }
        Long userId = UserInfoUtils.getUserId();
        Integer enabled = BooleanUtils.isTrue(isEnabled) ? ONE : ZERO;
        return userAlarmClockConfigDao.updateEnabledStatus(configId, userId, enabled) > ZERO;
    }
}
